﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Server;
using System.IO;

namespace ClinicalMassageSystem
{
    public partial class BackupRestoreScreen : Form
    {
        public BackupRestoreScreen()
        {
            InitializeComponent();
            UpdateListBox();
        }

        private void UpdateListBox()
        {
            lstBoxRestore.Items.Clear();
            DirectoryInfo directory = new DirectoryInfo(@"C:\Program Files\Microsoft SQL Server\MSSQL10.PJBSQLSERVER2008\MSSQL\Backup");
                          //new DirectoryInfo(@"C:\Program Files\Microsoft SQL Server\MSSQL10.SQLSERVER\MSSQL\Backup");
            if (directory.Exists)
            {
                FileInfo[] files = directory.GetFiles();
                foreach (FileInfo f in files)
                {
                    lstBoxRestore.Items.Add(f);
                }
            }
            else
            {
                lstBoxRestore.Items.Add("Backup directory does not exist:");
                lstBoxRestore.Items.Add(directory.ToString());
            }
        }
        private void btnCreateBackup_Click(object sender, EventArgs e)
        {
            bool successfulBackup = false;
            try
            {
                // Gets the SQL Server
                Server server = BackupRestore.GetSqlServer(txtServerName.Text, false, null, null);

                // Gets the Database from the SQL Server previoulsy obtained
                Database database = BackupRestore.GetDatabase(server, @"77B12F2B88A6DAFD161378DC247E0B83_SUAL STUDIO 2010\PROJECTS\CLINICALMASSAGESYSTEM\CLINICALMASSAGESYSTEM\BIN\DEBUG\CMSdatabase.mdf");

                // Performs Backup
                successfulBackup = BackupRestore.PerformBackup(server, database);

                //Update Backup listbox
                UpdateListBox();
            }
            catch (Exception ex)
            {
                successfulBackup = false;
                MessageBox.Show("Backup Failed because: " + ex.Message.ToString(), "Database Backup", 
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            finally
            {
                if (successfulBackup)
                {
                    MessageBox.Show("Database Backup was Successful", "Database Backup",
                        MessageBoxButtons.OK, MessageBoxIcon.Information);
                    UpdateListBox();
                }
                else
                {
                    MessageBox.Show("Database Backup was Unsuccessful", "Database Backup",
                        MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void btnCreateRestore_Click(object sender, EventArgs e)
        {
            bool successfulRestore = false;
            try
            {
                // Gets the SQL Server
                Server server = BackupRestore.GetSqlServer(txtServerName2.Text, false, null, null);

                // Gets the Database from the SQL Server previoulsy obtained
                Database database = BackupRestore.GetDatabase(server, "CMSDatabase");

                // To restore we need to obtain the file that our PerformBackup created, we can do this by
                // creating first a BackupDeviceItem with the name of the file
                BackupDeviceItem item =
                    BackupRestore.GetBackupDeviceItem(lstBoxRestore.SelectedItem.ToString());

                // The PerformRestore method takes the following arguments:
                // Server, BackupDeviceItem, Database, and a string with the Original Database name
                successfulRestore =
                    BackupRestore.PerformRestore(server, item, database, "CMSDatabase");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Restore Failed because: " + ex.Message.ToString(), "Database Restore",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            finally
            {
                if (successfulRestore)
                {
                    MessageBox.Show("Database Restore was Successful", "Database Restore",
                        MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("Database Restore was Unsuccessful", "Database Restore",
                        MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void BackupRestoreScreen_Load(object sender, EventArgs e)
        {

        }
    }
}
